//
//  MahjongGameActionCell.m
//  Mahjong
//
//  Created by 饶首建 on 17/7/25.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "MahjongGameActionCell.h"

@interface MahjongGameActionCell()

@property (weak, nonatomic) IBOutlet UIImageView *leftIcon;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;

@end

@implementation MahjongGameActionCell

- (void)setActionModel:(ActionModel *)am{
    //图标
    if ([am.actionType isEqualToString:@"胡"]) {
        if ([am.owner.playerId isEqual:_owner.playerId]) {
            _leftIcon.image = UIImageWithName(@"icon_hu_self");
        }else{
            _leftIcon.image = UIImageWithName(@"icon_hu_other");
        }
    }else if([am.actionType isEqualToString:@"杠"]){
        if ([am.owner.playerId isEqual:_owner.playerId]) {
            _leftIcon.image = UIImageWithName(@"icon_gang_self");
        }else{
            _leftIcon.image = UIImageWithName(@"icon_gang_other");
        }
    }else if([am.actionType containsString:@"查"]){
        if ([am.owner.playerId isEqual:_owner.playerId]) {
            _leftIcon.image = UIImageWithName(@"icon_cha_self");
        }else{
            _leftIcon.image = UIImageWithName(@"icon_cha_other");
        }
    }
    //内容
    _rightLabel.attributedText = [MahjongGameActionCell setLabelContent:am owner:_onlyMe?_owner:nil isSelf:[am.owner.playerId isEqual:_owner.playerId]];
}

+ (NSInteger)totalScore:(ActionModel *)am{
    //1.returned 返雨 BOOL
    //2.杠、胡、查
    //3.target
    //4.cagua
    //5.transferTo 转雨
    NSInteger totalScore = 0;
    if ([am.actionType containsString:@"杠"]) {
        if (!am.returned) { //未返雨，反雨则为0
            if (am.caguaArr.count) {
                totalScore = am.score + (am.score/2)*am.caguaArr.count;
            }else{
                totalScore = am.score * am.targetsArr.count;
            }
        }
    }else {
        totalScore = am.score * am.targetsArr.count;
    }
    return totalScore;
}

+(NSInteger)scoreAboutMe:(ActionModel*)am owner:(PlayerModel*)owner{
    NSInteger totalScore = 0;
    NSString *playerId = owner.playerId;
    if ([am.actionType containsString:@"杠"]) {
        if (!am.returned) { //未返雨
            if (am.transferedTo) {//转雨
                if ([am.transferedTo.playerId isEqual:playerId]) {//转雨目标
                    if ([am.caguaPlayerIdArr containsObject:playerId]){
                        totalScore = am.score * am.targetsArr.count + (am.score/2)*(am.caguaArr.count-1);
                    }else if ([am.targetPlayerIdArr containsObject:playerId]){
                        totalScore = am.score * (am.targetsArr.count - 1) + (am.score/2)*am.caguaArr.count;
                    }else{
                        totalScore = am.score * am.targetsArr.count + (am.score/2)*am.caguaArr.count;
                    }
                }else if([am.owner.playerId isEqual:playerId]){//被转雨的action主人
                    totalScore = 0;
                }else{ //被连带的人
                    if ([am.caguaPlayerIdArr containsObject:playerId]){
                        totalScore = -am.score/2;
                    }else if ([am.targetPlayerIdArr containsObject:playerId]){
                        totalScore = -am.score;
                    }
                }
            }else if ([am.caguaPlayerIdArr containsObject:playerId]) {
                totalScore = -am.score / 2;
            }else if ([am.targetPlayerIdArr containsObject:playerId]){
                totalScore = -am.score;
            }else if([am.owner.playerId isEqual:playerId]){
                totalScore = am.score * am.targetsArr.count + (am.score/2)*am.caguaArr.count;
            }
        }
    }else{
        if ([am.owner.playerId isEqual:playerId]) {
            totalScore = am.score * am.targetsArr.count;
        }else{
            totalScore = -am.score;
        }
    }
    
    return totalScore;
}


/**
 传入owner则为自己， 否则为全部
 */
+ (NSMutableAttributedString *)setLabelContent:(ActionModel *)am owner:(PlayerModel *)owner isSelf:(BOOL)isSelf{
    NSMutableAttributedString *allStr = [[NSMutableAttributedString alloc] initWithString:@""];
    UIColor *color = [UIColor whiteColor];
    if (isSelf) {
        if ([am.actionType isEqualToString:@"胡"]) {
            color = UIColorHex(FC3030);
        }else if([am.actionType isEqualToString:@"杠"]){
            color = UIColorHex(7ED321);
        }else{
            color = UIColorHex(F7E53B);
        }
    }
    
    //1.分
    NSInteger score = owner?[self.class scoreAboutMe:am owner:owner]:[self.class totalScore:am];
    UIColor *tmpColor;
    NSString *scoreStr;
    if (score > 0) {
        tmpColor = UIColorHex(F15A5A);
        scoreStr = NSStringFormat(@"+%ld",score);
    }else if (score == 0){
        tmpColor = UIColorHex(9B9B9B);
        scoreStr = @"0";
    }else{
        tmpColor = UIColorHex(519B00);
        scoreStr = NSStringFormat(@"%ld",score);
    }
    NSAttributedString *scoreAS = [[NSAttributedString alloc]initWithString:scoreStr attributes:@{NSForegroundColorAttributeName:owner?tmpColor:color}]; //NSFontAttributeName
    [allStr appendAttributedString:scoreAS];
    [allStr appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]];
    
    //.分desc
    NSString *desc = @"";
    if (am.returned){
        //返雨
        desc = @"(返雨)";
    }else if (am.transferedTo){
        //转雨
        desc = @"(转雨)";
    }
    if (desc.length) {
        NSAttributedString *descAS = [[NSAttributedString alloc]initWithString:desc attributes:@{NSForegroundColorAttributeName:color}]; //NSFontAttributeName
        [allStr appendAttributedString:descAS];
        [allStr appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]];
    }
    //2.action owner
    NSString *amOwnerName = am.owner.playerName;
    NSAttributedString *amOwnerNameAS = [[NSAttributedString alloc]initWithString:amOwnerName attributes:@{NSForegroundColorAttributeName:color}];
    [allStr appendAttributedString:amOwnerNameAS];
    [allStr appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]];
    //3.动作
    NSString *actionName = am.actionType;
    NSAttributedString *actionNameAS = [[NSAttributedString alloc]initWithString:actionName attributes:@{NSForegroundColorAttributeName:color}];
    [allStr appendAttributedString:actionNameAS];
    [allStr appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]];
    //4.对象
    NSString *targetStr = @"";
    if (am.targetsArr.count > 2) {
        targetStr = @"三家";
    }else{
        targetStr = [am.targetPlayerNameArr componentsJoinedByString:@"、"];
    }
    NSAttributedString *targetStrAS = [[NSAttributedString alloc]initWithString:targetStr attributes:@{NSForegroundColorAttributeName:color}];
    [allStr appendAttributedString:targetStrAS];
    [allStr appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]];
    //5.翻型
    NSString *attrStr = [am.attributesArr componentsJoinedByString:@"、"];
    NSAttributedString *attrStrAS = [[NSAttributedString alloc]initWithString:attrStr attributes:@{NSForegroundColorAttributeName:color}];
    [allStr appendAttributedString:attrStrAS];
    [allStr appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]];
    //6.其他：转雨等
    NSString *addStr = @"";
    NSAttributedString *addStrAS = [[NSAttributedString alloc]initWithString:addStr attributes:@{NSForegroundColorAttributeName:color}];
    [allStr appendAttributedString:addStrAS];
    [allStr appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]];
    //7.可编辑图标
    if (isSelf) {
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        attch.image = [UIImage imageNamed:@"edit"];
        attch.bounds = CGRectMake(0, 0, 22, 22);
        NSAttributedString *attchStr = [NSAttributedString attributedStringWithAttachment:attch];
        [allStr appendAttributedString:attchStr];
    }

    return allStr;
}


+ (float)height:(ActionModel*)am owner:(PlayerModel *)owner isSelf:(BOOL)isSelf{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 216, 32)];
    label.numberOfLines = 0;
    label.font = DefaultSystemFont(20);
    
    label.attributedText = [self setLabelContent:am owner:owner isSelf:isSelf];
    
    CGSize sizeToFit = [label sizeThatFits:CGSizeMake(216, MAXFLOAT)];
    return sizeToFit.height + 12 + 4;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
